//
//  ZToolsVC.m
//  WeClubs
//
//  Created by Zed Link on 26/3/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZToolsVC.h"
#import "ZToolsItem.h"
#import "ZToolsHeader.h"
#import <SDCycleScrollView.h>

#import "ZAssignNotifyVC.h"
#import "ZAssignMeetingVC.h"
#import "ZAssignMissionVC.h"
#import "ZClubSettingVC.h"

#define PANEL_HEADER_HEIGHT 40
#define PANEL_BOTTOM_SPACE 8

NSString * const ToolsItemReuseID = @"ZToolsItemReuseID";
NSString * const ToolsHeaderReuseID = @"ZToolsHeaderReuseID";

@interface ZToolsVC () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

/** 功能说明轮播 */
@property (weak, nonatomic) SDCycleScrollView *imgaeRotater;

/** 组织管理-功能面板 */
@property (weak, nonatomic) UICollectionView *managePanel;
@property (strong, nonatomic) NSArray *manageData;

/** 组织资料-功能面板 */
@property (weak, nonatomic) UICollectionView *mediaPanel;
@property (strong, nonatomic) NSArray *mediaData;

@end

@implementation ZToolsVC

#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    self.navigationItem.title = @"工具";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithImage:@"navItem_message_blue" shouldOriginal:YES andSelector:@selector(messageAction)];
}

- (void)messageAction
{
    // Todo: 跳转 <消息中心>
}

#pragma mark - View Life Cycle

- (void)zLoadSubview
{
    [super zLoadSubview];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // ========== 功能说明轮播 ==========
    NSArray *imageNames = @[@"Tools_PlaceHolder"];
    SDCycleScrollView *imageRotater = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero imageNamesGroup:imageNames];
    
    self.imgaeRotater = imageRotater;
    [self.view addSubview:imageRotater];
    CGFloat height = self.view.width / 750.f * 320.f;
    [imageRotater mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(64);
        make.height.mas_equalTo(height);
    }];
    
    // ========== 布局计算 ==========
    CGFloat itemWidth = self.view.width / TOOLS_ITEMS_OF_ROW;
    CGFloat itemHeight = 81;
    
    // ========== 组织管理-功能面板 ==========
    NSUInteger itemCount = self.manageData.count;
    NSUInteger rowCount = itemCount / TOOLS_ITEMS_OF_ROW;
    if (itemCount % TOOLS_ITEMS_OF_ROW != 0) {
        rowCount++;
    }
    
    UICollectionView *managePanel = [self addFunctionPanelWithItemSize:CGSizeMake(itemWidth, itemHeight) AndData:self.manageData];
    self.managePanel = managePanel;
    [self.view addSubview:managePanel];
    [managePanel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(imageRotater.mas_bottom).offset(12);
        make.height.mas_equalTo(itemHeight * rowCount + PANEL_HEADER_HEIGHT + PANEL_BOTTOM_SPACE);
    }];
    
    // ========== 组织资料-功能面板 ==========
    NSUInteger mediaItemCount = self.mediaData.count;
    NSUInteger mediaRowCount = mediaItemCount / TOOLS_ITEMS_OF_ROW;
    if (mediaItemCount % TOOLS_ITEMS_OF_ROW != 0) {
        mediaRowCount++;
    }

    UICollectionView *mediaPanel = [self addFunctionPanelWithItemSize:CGSizeMake(itemWidth, itemHeight) AndData:self.mediaData];
    self.mediaPanel = mediaPanel;
    [self.view addSubview:mediaPanel];
    [mediaPanel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.managePanel.mas_bottom).offset(12);
        make.height.mas_equalTo(itemHeight * mediaRowCount + PANEL_HEADER_HEIGHT + PANEL_BOTTOM_SPACE);
    }];
}

#pragma mark - Private Method

- (UICollectionView *)addFunctionPanelWithItemSize:(CGSize)size AndData:(NSArray *)data
{
    // 每个CollectionView都要持有自己的Layout对象, 不能共用, 否则布局会有莫名的问题
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = size;
    
    UICollectionView *functionPanel = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [functionPanel setBackgroundColor:[UIColor whiteColor]];
    functionPanel.delegate = self;
    functionPanel.dataSource = self;
    functionPanel.bounces = NO;
    
    [functionPanel registerClass:[ZToolsItem class] forCellWithReuseIdentifier:ToolsItemReuseID];
    [functionPanel registerClass:[ZToolsHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ToolsHeaderReuseID];

    return functionPanel;
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.managePanel) {
        return self.manageData.count;
    }
    
    if (collectionView == self.mediaPanel) {
        return self.mediaData.count;
    }
    
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.managePanel) {
        ZToolsItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:ToolsItemReuseID forIndexPath:indexPath];
        NSArray *data = self.manageData[indexPath.row];
        [item.button setTitle:data[0] andImage:[UIImage imageNamed:data[1]]];
        
        return item;
    }
    
    if (collectionView == self.mediaPanel) {
        ZToolsItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:ToolsItemReuseID forIndexPath:indexPath];
        NSArray *data = self.mediaData[indexPath.row];
        [item.button setTitle:data[0] andImage:[UIImage imageNamed:data[1]]];
        
        return item;
    }
    
    return nil;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (self.managePanel == collectionView) {
        ZToolsHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ToolsHeaderReuseID forIndexPath:indexPath];
        [header setDescriptionTitle:@"指派事项"];
        
        return header;
    }
    
    if (self.mediaPanel == collectionView) {
        ZToolsHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ToolsHeaderReuseID forIndexPath:indexPath];
        [header setDescriptionTitle:@"组织管理"];
        
        return header;
    }
    
    return nil;
}

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.width, PANEL_HEADER_HEIGHT);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *data;
    if (self.managePanel == collectionView) {
         data = self.manageData[indexPath.row];
    }
    
    if (self.mediaPanel == collectionView) {
        data = self.mediaData[indexPath.row];
    }
    
    Class cls = NSClassFromString(data[2]);
    NSString *clsErrDesc = @"找不到该名称对应的控制器! 请检查类名是否正确";
    
    UIViewController *vc = [cls zNew];
    NSAssert([vc isKindOfClass:[UIViewController class]], clsErrDesc);
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Getter & Setter

- (NSArray *)manageData
{
    if (_manageData == nil) {
        _manageData = @[
                           @[@"通知管理", @"Notify", @"ZAssignNotifyVC"],
                           @[@"会议管理", @"Meeting", @"ZAssignMeetingVC"],
                           @[@"任务管理", @"Mission", @"ZAssignMissionVC"],
//                           @[@"活动管理", @"Activity", @"ZAssignActivityVC"],
//                           @[@"组织事项", @"TodoItem", @""],
//                           @[@"组织设置", @"Setting", @"ZClubSettingVC"],
//                           @[@"创建组织", @"Create", @""],
//                           @[@"投票管理", @"Vote", @""],
                           ];
    }
    return _manageData;
}

- (NSArray *)mediaData
{
    if (_mediaData == nil) {
        _mediaData = @[
//                          @[@"组织文档", @"Document", @""],
//                          @[@"组织相册", @"Album", @""],
                            @[@"组织设置", @"Setting", @"ZClubSettingVC"],
                            @[@"创建组织", @"Create", @"ZClubCreateVC"],
                          ];
    }
    return _mediaData;
}

@end
